package cn.edu.java.io.inputstream.filterinputstream;

import lombok.SneakyThrows;

import java.io.*;

public class PushbackInputStreamDemo {
    @SneakyThrows
    public static void main(String[] args) {
        String message = "Hello, World!";
        byte[] bytes = message.getBytes();

        // 创建一个 ByteArrayInputStream 对象
        ByteArrayInputStream input = new ByteArrayInputStream(bytes);

        // 创建一个 PushbackInputStream 对象，使用上述 ByteArrayInputStream 作为输入流
        PushbackInputStream pushbackInput = new PushbackInputStream(input);

        int byteRead;
        while ((byteRead = pushbackInput.read()) != -1) {
            // 输出读取的字节
            System.out.print((char) byteRead);

            if (byteRead == ',') {
                // 推回刚读取的逗号字节
                pushbackInput.unread(byteRead);
                // 继续读取并输出下一个字节
                byteRead = pushbackInput.read();
                System.out.print((char) byteRead);
            }
        }

        // 关闭流
        pushbackInput.close();
        input.close();
    }
}

